package com.sirun.ble.utils.sdk;

import static com.sirun.ble.constants.BleConfig.BleSdkTag;
import static com.sirun.ble.constants.BleConfig.CONTROL_STEP_CMD_SEND;
import static com.sirun.ble.constants.BleConfig.CONTROL_STEP_FAIL;
import static com.sirun.ble.constants.BleConfig.CONTROL_STEP_SUCCESS;
import static com.sirun.ble.constants.BleConfig.CONTROL_STEP_UNDER_CONTROL;
import static com.sirun.ble.constants.BleConfig.TAG_CONNECTED;
import static com.sirun.ble.constants.BleConfig.TAG_CONNECTED_AUTH;
import static com.sirun.ble.constants.BleConfig.TAG_CONNECTED_NONE;

import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;

import com.sirun.ble.constants.BleConfig;
import com.sirun.ble.enums.BleEnum;
import com.sirun.ble.listeners.OnBleConnectListener;
import com.sirun.ble.listeners.OnBleControlListener;
import com.sirun.ble.utils.AwDataUtil;
import com.sirun.ble.utils.AwLog;

/**
 * @author : hzw
 */
public class AwBleCallbackUtil {

    public static void connectCallback(OnBleConnectListener callback, BleEnum errorEnum, int tag, BluetoothDevice device) {
        if(callback == null) {
            AwLog.d(BleSdkTag, "BleCallbackUtil connectCallback is null, return");
            return;
        }
        switch (tag) {
            case TAG_CONNECTED_NONE:
                if(!BleConfig.isSystemBleOpen) {
                    AwLog.d(BleSdkTag, "BleCallbackUtil connectCallback isSystemBleOpen false..., return");
                    return;
                }
                callback.connectFail(device, errorEnum.code, errorEnum.desc);
                break;
            case TAG_CONNECTED:
                callback.connected(device, TAG_CONNECTED);
                break;
            case TAG_CONNECTED_AUTH:
                callback.connected(device, TAG_CONNECTED_AUTH);
                break;
        }
    }

    public static void controlCallback(OnBleControlListener callback, BleEnum errorEnum, int type, int controlStep, String msg) {
        if(!BleConfig.isSystemBleOpen) {
            AwLog.d(BleSdkTag, "BleCallbackUtil controlCallback isSystemBleOpen false..., return");
            return;
        }
        if(callback == null) {
            AwLog.d(BleSdkTag, "BleCallbackUtil controlCallback is null, return");
            return;
        }
        switch (controlStep) {
            case CONTROL_STEP_CMD_SEND:
                callback.sendCmdSuccess(type);
                break;
            case CONTROL_STEP_UNDER_CONTROL:
                callback.underControl();
                break;
            case CONTROL_STEP_SUCCESS:
                callback.controlSuccess(type);
                break;
            case CONTROL_STEP_FAIL:
                if(AwDataUtil.isEmpty(msg)) {
                    callback.controlFail(type, errorEnum.code, errorEnum.desc);
                } else {
                    callback.controlFail(type, errorEnum.code, msg);
                }
                break;
        }
    }
}
